<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <!-- <el-card style="margin-top:-10px;">
        <div slot="header" class="clearfix">
          <span style="color:#329dff;font-weight:bold;font-size:18px;">新建工程</span>
        </div> -->

        <el-card style="margin-top:-2px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">工程信息</span>
          </div>

          <el-row>
              <el-col :span="12">
                <el-form-item label="工程编码" prop="engineerNo">
                  <el-input v-model="form.engineerNo" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="工程代码" prop="code">
                  <el-input v-model="form.code" />
                </el-form-item>
              </el-col>
          </el-row>

          <el-row>
              <el-col :span="12">
                <el-form-item label="工程名称" prop="name">
                  <el-input v-model="form.name" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="工程金额" prop="costConstruction">
                  <el-input-number style="width:95%;color:red;" v-model="form.costConstruction" :controls="false" :precision="2" /><span style="color:blue;margin-left:5px;">万元</span>
                </el-form-item>
              </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="所属项目" prop="projectId">
                <el-select v-model="form.projectId" filterable clearable placeholder="请选择所属项目" style="width:100%;">
                  <el-option v-for="item in projectList" :key="item.uuid" :label="item.name" :value="item.uuid"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="启动日期" prop="startTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择启动日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="竣工日期" prop="endTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择竣工日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人" prop="chargerId">
                <el-select v-model="form.chargerId" filterable placeholder="请选择负责人" style="width:100%;">
                  <el-option v-for="item in participants" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责部门" prop="deptId">
                <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择负责部门" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
            <el-form-item label="施工单位" prop="ownerDept">
              <el-input v-model="form.ownerDept" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="监理单位" prop="supervisor">
                <el-input v-model="form.supervisor" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
            <el-form-item label="施工联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactPhone">
                <el-input v-model="form.contactPhone" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="施工地点" prop="constructSite">
                <el-input v-model="form.constructSite" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="工程描述" prop="describe">
                <el-input type="textarea" v-model="form.describe" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="补充说明" prop="remark">
                <el-input type="textarea" v-model="form.remark" :rows="6"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card style="margin-top:10px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">挂接子工程</span>
          </div>
          <el-table ref="table" :data="subEngineerList" @selection-change="handleSelectionChange" border :row-key="getRowKeys" style="margin-top:10px;">
            <el-table-column type="selection" width="50" align="center" :selectable="selectable" />
            <el-table-column label="名称" align="center" prop="name" />
            <el-table-column label="代码" align="center" prop="code" />
            <el-table-column label="类型" align="center" prop="engineerTypeName">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_sub_engineer_type" :value="scope.row.engineerType"/>
              </template>
            </el-table-column>
            <el-table-column label="所属工程" align="center" prop="engineerName" />
            <el-table-column label="所属项目" align="center" prop="projectName" />
            <el-table-column label="金额(万元)" align="center" prop="costConstruction">
              <template slot-scope="scope">
                <span style="color:blue;">{{ scope.row.costConstruction | moneyFormat }}</span>
              </template>
            </el-table-column>
            <el-table-column label="施工单位" align="center" prop="ownerDept" />
            <el-table-column label="监理单位" align="center" prop="supervisor" />
            <el-table-column label="状态" align="center" prop="status">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_sub_engineer_status" v-if="scope.row.status==1" style="color:red;" :value="scope.row.status"/>
                <dict-tag :options="dict.type.sys_sub_engineer_status" v-if="scope.row.status==2" style="color:blue;" :value="scope.row.status"/>
                <dict-tag :options="dict.type.sys_sub_engineer_status" v-if="scope.row.status==3" style="color:green;" :value="scope.row.status"/>
                <dict-tag :options="dict.type.sys_sub_engineer_status" v-if="scope.row.status==4" style="color:gray;" :value="scope.row.status"/>
              </template>
            </el-table-column>
          </el-table>
         </el-card>

        <el-card style="margin-top:10px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">工程附件</span>
          </div>
          <el-row>
              <el-col :span="24">
                <el-form-item prop="contractAttach">
                  <image-upload-custom v-model="form.contractAttach" :limit="10000" :imageName="imageName"></image-upload-custom>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item prop="materialAttach">
                  <file-upload-custom
                    :fileSize="10000"
                    :attachName="attachOtherName"
                    :fileList_show="fileList_show"
                    v-model="form.materialAttach"
                    @addlist="fileList_show_add"
                    @deletlist="fileList_show_delet"
                  />
                </el-form-item>
              </el-col>
            </el-row>
        </el-card>

      <!-- </el-card> -->
    </el-form>
    <center>
      <div slot="footer" class="dialog-footer" style="margin-top:20px;">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>
    </center>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listAllUser } from "@/api/system/user";
import { getProjectList } from "@/api/system/project";
import { createEngineerNo,editEngineer } from "@/api/system/engineer";
import { getSubEngineerList4Engineer } from "@/api/system/subengineer";

export default {
  name: "NewEngineer",
  dicts: ['sys_sub_engineer_status','sys_sub_engineer_type'],
  components:{ Treeselect },
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      attachPdfName: '上传PDF',
      attachOtherName: '上传文件',
      imageName: '上传图片',
      fileList_show:[],
      participants: [],
      deptOptions: [],
      projectList: [],
      subEngineerList: [],
      form: {},
      rules: {

      },
    };
  },
  created() {
    this.createEngineerNo();
    this.listAllUser();
    this.getTreeselect();
    this.getProjectList();
    this.getSubEngineerList4Engineer();
  },
  mounted(){
    this.defaultChecked() // 该方法可以在拿到表格数据后进行调用
  },
  methods: {
    selectable(row, index) {
      if (row.hasEngineer == 1) {
        return false; // 禁用
      } else {
        return true; //不禁用
      }
    },
    defaultChecked(){
      var that = this;
      that.$nextTick(() => {
          this.subEngineerList.forEach((v, i) => {
              if (v.hasEngineer == 1) {
                  this.$refs.table.toggleRowSelection(v, true);
              }
          });
      });
    },
    getRowKeys(row) {
      return row.uuid;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    getSubEngineerList4Engineer(){
      let query = {};
      getSubEngineerList4Engineer(query).then(res =>{
        this.subEngineerList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.createEngineerNo();
    },
    createEngineerNo(){
      createEngineerNo().then(res =>{
        this.reset();
        this.form.engineerNo = res.data.engineerNo;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        engineerNo: null,
        name: null,
        projectId: null,
        code: null,
        startTime: null,
        endTime: null,
        describe: null,
        status: null,
        chargerId: null,
        deptId: null,
        contactPerson: null,
        contactPhone: null,
        costConstruction: null,
        ownerDept: null,
        ownerContact: null,
        supervisor: null,
        constructSite: null,
        contractAttach: null,
        materialAttach: null,
        roleIds: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        subEngineerIds: null,
      };
      this.resetForm("form");
    },
    listAllUser(){
      listAllUser().then(res =>{
        this.participants = res.data;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.ids.length>0){
            this.form.subEngineerIds = this.ids.join(",");
          }
          if (this.form.uuid != null) {
            editEngineer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$router.push({path:"/engineer/queryengineer"});
              this.reset();
            });
          } else {
            editEngineer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$router.push({path:"/engineer/queryengineer"});
              this.reset();
            });
          }
        }
      });
    },
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getProjectList(){
      getProjectList().then(res =>{
        this.projectList = res.data;
      })
    },
    // 文件上传接收子组件值，同步（增）
    fileList_show_add(val){
      this.fileList_show = this.fileList_show.concat(val);
    },
    // 文件上传接收子组件值，同步（减）
    fileList_show_delet(val){
      this.fileList_show.splice(val, 1);
    },
  }
};
</script>
